package com.iasku.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.iasku.adapter.ErrorBookAdapter;
import com.iasku.constant.Constant;
import com.iasku.constant.IntentConfig;
import com.iasku.entity.UserErrorBook;
import com.iasku.iaskueleclass_phone.R;
import com.iasku.iaskueleclass_phone.ReturnData;
import com.iasku.iaskueleclass_phone.Status;
import com.iasku.iaskueleclass_phone.activity.ErrorBookQuestionActivity;
import com.iasku.manager.DataManager;
import com.iasku.manager.IDataManager;
import com.iasku.manager.IaskuManager;

public class ErrorbookFragment extends BaseFragment {
    private static final int TASK_GET_ERRORBOOK = 1;
    private ListView mErrorBookLv;
    private ArrayList<UserErrorBook> mErrorQueList;
    
    
    private void initView(View view){
        mErrorBookLv = (ListView) view.findViewById(R.id.errorbook_listview);
        addListener();
        startTask(TASK_GET_ERRORBOOK);
    }
    
    private void addListener(){
        mBarSearchBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startTask(TASK_GET_ERRORBOOK);
            }
        });
        
        mErrorBookLv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(getActivity().getApplicationContext(), ErrorBookQuestionActivity.class);
                intent.putParcelableArrayListExtra(IntentConfig.ERROR_BOOK_LIST, mErrorQueList);
                intent.putExtra(IntentConfig.ERROR_BOOK_POSITION, position);
                startActivity(intent);
            }
        });
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.errorbook_fragment_layout, null);
        initTitleBar(R.string.errorbook_title);
        initView(mRootView);
        return mRootView;
    }
    
    @Override
    public int onTaskComplete(int taskId, Status status) {
        mProgressDialog.dismiss();
        switch (taskId) {
        case TASK_GET_ERRORBOOK:{
            if(status.code == 0){
                if(mErrorQueList != null && !mErrorQueList.isEmpty()){
                    mErrorBookLv.setVisibility(View.VISIBLE);
                    ErrorBookAdapter adapter = new ErrorBookAdapter(getActivity().getApplicationContext(),mErrorQueList);
                    mErrorBookLv.setAdapter(adapter);
                }
            }
        }
        break;
        }
        return super.onTaskComplete(taskId, status);
    }
    
    @Override
    public int onTaskStart(int taskId, Bundle bundle) {
        if(taskId == TASK_GET_ERRORBOOK){
            mErrorBookLv.setVisibility(View.INVISIBLE);
            mProgressDialog.show();
        }
        return super.onTaskStart(taskId, bundle);
    }
    
    @Override
    public Status onTaskInBackground(int taskId, Bundle bundle) {
        // TODO Auto-generated method stub
        switch (taskId) {
        case TASK_GET_ERRORBOOK:{
            IDataManager manager = DataManager.getInstance();
            initTime();
            ReturnData<ArrayList<UserErrorBook>> mReturnData = manager.getErrorQuestion(Constant.USER_IASKUNAME,
                    IaskuManager.getInstance().getSubjectId(),
                    String.valueOf(time_start),String.valueOf(time_end));
            mErrorQueList = mReturnData.getData();
            return new Status(mReturnData);
        }
        }
        return super.onTaskInBackground(taskId, bundle);
    }
}
